﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.UI;
using DAL;

namespace Crad.Controllers
{
    public class AdminPageController : Controller
    {

        CardEntities db = new CardEntities();

        // GET: AdminPage
        public ActionResult PageHome()
        {
            return View();
        }


        public ActionResult ApplyCard()
        {
            return View();
        }


        //开户
        public ActionResult UserApplyCard(string Name, string card, string phone)
        {
            Random rd = new Random();
            UserCrad cardEntity = new UserCrad()
            {
                CradStatic = 0,
                Money = 0,
                Name = Name,
                Phone = phone,
                IdentityCrad = card,
                ID = rd.Next(123456789, 999999999).ToString()
            };
            db.UserCrad.Add(cardEntity);
            int i = db.SaveChanges();
            if (i > 0)
            {
                return Content("<script> alert('添加成功');  window.location.reload() </script> ");
            }
            else
            {
                return Content("alert('添加失败');   window.location.reload() </script> ");
            }
        }

        //充值
        public ActionResult TopUp()
        {
            return View();
        }


        //充值实现
        public ActionResult TopUpDAL(string card,double money)
        {
            UserCrad en=  db.UserCrad.FirstOrDefault(s => s.ID.Equals(card));
            if (en == null)
            {
                return Json(new { b = false, msg = "卡号不存在" });
            }
            en.Money += (Decimal) money;
            Running running = new Running()
            {
                Money = (Decimal)money,
                 Remark="用户充值",
                  RunningStatic=true,
                   UserCradId=en.ID,
                    RunningTime=DateTime.UtcNow
            };
            db.Running.Add(running);
            if (db.SaveChanges() > 0)
            {
                return Json(new { b = true, msg = string.Format("卡号:{0}充值金额{1}当前余额{2}",en.ID,money,en.Money)});
            }
            else
            {
                return Json(new { b = false, msg = "发送未知错误" });
            }
        }


        //挂失页面
        public ActionResult GS()
        {           
            return View(db.UserCrad.ToList());
        }

        //挂失操作
        public ActionResult GSs(string card,int cardstatic)
        {
            UserCrad user= db.UserCrad.FirstOrDefault(s => s.ID.Equals(card));
            if (user == null)
            {
                return Json(new { f = false, mes = "卡号不存在" });
            }
            string mess = cardstatic == 0 ? "成功挂失" : "成功取消挂失";


            user.CradStatic = cardstatic;
            db.SaveChanges();

            return Json(new { f = true, mes = mess });


            
        }

        //查看流水
        public ActionResult QuenyRunning(string card)
        {
            List<Running> list =  db.Running.Where(s => s.UserCradId.Equals(card)).OrderBy(s => s.RunningTime).ToList();
            if(list.Count <= 0)
            {
                return Json(new { f = false, mes = "卡号不存在或者卡号暂时没有流水" });
            }
            return Json(new { f=true,data=list });

        }

        public ActionResult XiaoFeiPafe()
        {
            return View();
        }

        //消费金额
        public ActionResult XiaoFei(string card, double money,string rameak)
        {
            UserCrad en = db.UserCrad.FirstOrDefault(s => s.ID.Equals(card));
            if (en == null)
            {
                return Json(new { b = false, msg = "卡号不存在" });
            }
            money = money >= 0 ? -money : money;
            en.Money += (Decimal)money;
            Running running = new Running()
            {
                Money = (Decimal)money,
                Remark = rameak,
                RunningStatic = false,
                UserCradId = en.ID,
                RunningTime = DateTime.UtcNow
            };
            db.Running.Add(running);
            if (db.SaveChanges() > 0)
            {
                return Json(new { b = true, msg = string.Format("卡号:{0}消费金额{1}当前余额{2} 消费备注{3}", en.ID, money, en.Money,rameak) });
            }
            else
            {
                return Json(new { b = false, msg = "发送未知错误" });
            }

        }


    }
}